/*
 * @Author: huangyq
 * @Date: 2022-04-24 16:35:24
 * @LastEditTime: 2022-04-24 16:52:30
 * @LastEditors: huangyq
 * @Description: 时间工具
 * @FilePath: /vue3-boilerplate-pc-client/src/utils/dateUtils.ts
 */

const totalHour = 60 * 60 * 1000
const totalDay = 24 * totalHour
const totalWeek = 7 * totalDay

export function calculateInterVal(timestamp: number): string | number {
  const interval = Date.now() - timestamp
  if (interval < 0) return interval
  if (interval < totalHour) {
    return '近一小时内'
  } else if (interval < totalDay) {
    return Math.ceil(interval / totalHour) + '小时前'
  } else if (interval < totalWeek) {
    return Math.ceil(interval / totalDay) + '天前'
  } else {
    return new Date(timestamp).toLocaleString()
  }
}
